package com.example.todofinally;

import android.app.DatePickerDialog;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.DatePicker;
import android.widget.ImageButton;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

import com.example.todofinally.sql.Data;
import com.example.todofinally.sql.SpHelper;
import com.github.mikephil.charting.charts.PieChart;
import com.github.mikephil.charting.data.PieData;
import com.github.mikephil.charting.data.PieDataSet;
import com.github.mikephil.charting.data.PieEntry;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class StatisticsActivity extends AppCompatActivity {

    private ImageButton dateChoose_button;
    private ImageButton dateChoose_button2;
    private TextView StartTime;
    private TextView EndTime;
    private SpHelper spHelper;
    private List<Data> list;
    private int year;
    private int month;
    private int day;
    private long choose_start_time;
    private long choose_end_time;
    private TextView tvresult;

    private String result;
    private int status_0;
    private int status_1;
    private int status_2;

    private PieChart pieChart;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_statistics);
        dateChoose_button=findViewById(R.id.im_Start_time);
        dateChoose_button2=findViewById(R.id.im_EndTime);

        StartTime = findViewById(R.id.tv_start_time);
        EndTime = findViewById(R.id.tv_end_time);

        tvresult = findViewById(R.id.tv_result);
        pieChart = findViewById(R.id.pieChart);

        dateChoose_button2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                getDate(StatisticsActivity.this);
            }
            private void getDate(StatisticsActivity statisticsActivity) {
                Calendar calendar=Calendar.getInstance();
                new DatePickerDialog(statisticsActivity,
                        new DatePickerDialog.OnDateSetListener() {
                            @Override
                            public void onDateSet(DatePicker datePicker, int year, int month, int day) {
                                choose_end_time = year*10000+(month+1)*100+day;
                                String str_year = String.valueOf(year);
                                String str_month = String.valueOf(month+1);
                                String str_day = String.valueOf(day);
                                String end_time = str_year + "年" + str_month+ "月" +str_day +"日";
                                EndTime.setText(end_time);
                                /*month++;
                                if(month<10&&day<10){
                                    editText.setText(year+"年"+"0"+month+"月"+"0"+day+"日");
                                } else if(month<10&&day>=10){
                                    editText.setText(year+"年"+"0"+month+"月"+day+"日");
                                }else if (month>=10&&day<10){
                                    editText.setText(year+"年"+month+"月"+"0"+day+"日");
                                }else {
                                    editText.setText(year+"年"+month+"月"+day+"日");
                                }*/
                                findData();
                            }
                        },calendar.get(Calendar.YEAR),calendar.get(Calendar.MONTH),calendar.get(Calendar.DAY_OF_MONTH)).show();
            }
        });

        dateChoose_button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                getDate(StatisticsActivity.this);
            }

            private void getDate(StatisticsActivity statisticsActivity) {
                Calendar calendar=Calendar.getInstance();
                new DatePickerDialog(statisticsActivity,
                        new DatePickerDialog.OnDateSetListener() {
                            @Override
                            public void onDateSet(DatePicker datePicker, int year, int month, int day) {
                                choose_start_time = year*10000+(month+1)*100+day;
                                String str_year = String.valueOf(year);
                                String str_month = String.valueOf(month+1);
                                String str_day = String.valueOf(day);
                                String state_time = str_year + "年" + str_month+ "月" +str_day +"日";
                                StartTime.setText(state_time);

                            }
                        },calendar.get(Calendar.YEAR),calendar.get(Calendar.MONTH),calendar.get(Calendar.DAY_OF_MONTH)).show();
            }
        });
    }
    private void findData() {
        status_0 = 0;
        status_1 = 0;
        status_2 = 0;
        spHelper=new SpHelper(StatisticsActivity.this);
        list = spHelper.query1();
        Map<String, Integer> map = new HashMap<>();
        result = "";
        for (Data data : list) {
            String date = data.getDate();
//            Log.i("date11", ""+data.getDate());
            if(date.length() >= 10) {
                year = Integer.parseInt(date.substring(0, 4));
                month = Integer.parseInt(date.substring(5, 7));
                day = Integer.parseInt(date.substring(8, 10));
            }
            long data_time = year*10000+month*100+day;
            if (choose_start_time<=data_time && data_time<=choose_end_time){
                map.put("状态",data.getStatus());
                if(data.getStatus() == 0){
                    status_0++;
                }else if(data.getStatus() == 1){
                    status_1++;
                }else if(data.getStatus() == 2){
                    status_2++;
                }
            }
        }
        result += "状态0："+status_0+" 状态1："+status_1+" 状态2："+status_2;
        tvresult.setText(result);

        pieChart.setUsePercentValues(false);
        ArrayList<PieEntry> list = new ArrayList<>();
        list.add(new PieEntry(status_0,"未完成"));
        list.add(new PieEntry(status_1,"已完成"));
        list.add(new PieEntry(status_2,"逾期"));

        PieDataSet pieDataSet = new PieDataSet(list,"");
        pieDataSet.setColors(Color.parseColor("#38B0DE"),Color.parseColor("#32CD32"),Color.parseColor("#BC1717"));
        PieData pieData = new PieData(pieDataSet);
        pieChart.setData(pieData);


//        Log.i("status", "0: "+status_0+" 1:"+status_1+" 2:"+status_2);
    }

}